package View.Game;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;

import Controller.Game.GamePanelController;
import Model.Visible.Fleet;
import Model.Visible.Planet;
import Utils.ActionButton;
import Utils.ButtonImage;
import Utils.ConstantsImplement;
import Utils.MediaUploder;
import Utils.Utils;
import View.GUI.FleetGUI;
import View.GUI.GameMenuPanel;
import View.GUI.PanelWalk;
import View.GUI.PlanetGUI;

/**
 * This Panel shows the game
 * @author magal
 *
 */
public class GamePanel extends JPanel implements ConstantsImplement{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Image imageBackground;
	private BufferedImage menu_;
	private PlanetGUI planets_[] = new PlanetGUI[NUM_PLANETS];
	private JLabel label;
	private GamePanelController gamePanelController_;
	private boolean littlePanel_;
	private ButtonImage menuButton_;
	private MediaUploder media_;
	private GameMenuPanel gameMenuPanel_;
	private boolean gamebuttonup_ = false;


	public GamePanel(GamePanelController gamePanelController, MediaUploder media) {
		// TODO Auto-generated constructor stub
		setLayout(null);
		media_ = media;
		gamePanelController_ = gamePanelController;

		imageBackground = media_.imageBackgroundGame_;
		menu_ = media_.menu_;
		//System.out.println(imageBackground.getWidth(null)+ " " + imageBackground.getHeight(null));
		Dimension a = new Dimension(imageBackground.getWidth(null),imageBackground.getHeight(null));
		setPreferredSize(a);
		setLocation(0, 0);
		
		menuButton_ = new ButtonImage(this, media_.menuButton1_, media_.menuButton1_, 0, 550);
		menuButton_.addActionButton(new ActionButton() {

			@Override
			public void enterAction() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void exitAction() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void click(MouseEvent paramMouseEvent) {
				// TODO Auto-generated method stub
				if(gamebuttonup_==false){
					System.out.println(gamebuttonup_);
					gameMenuPanel_ = new GameMenuPanel(GamePanel.this.media_);
					add(gameMenuPanel_);
					gamebuttonup_=true;
				}else{
					System.out.println(gamebuttonup_);
					remove(gameMenuPanel_);
					gamebuttonup_=false;
				}
			}
		});
		
		planets_ = new PlanetGUI[NUM_PLANETS];
		for (int i = 0; i < gamePanelController_.getPlanetGUI().length; i++){
			planets_[i] = gamePanelController_.getPlanetGUI()[i];
			addMouseListener(planets_[i]);
			addMouseMotionListener(planets_[i]);
		}
		
		
		label = new JLabel("AQUIIII");
		
		label.setSize(140, 150);
		
		label.setLocation(1500,1500);
		
		addMouseListener(menuButton_);
		add(label);
	}
	
	public void paintComponent(Graphics g){
		super.paintComponents(g);
		g.drawImage(imageBackground, 0, 0, this);
		//System.out.println(getWidth() +" " +getHeight());
		for (int i = 0; i < planets_.length; i++) {
			gamePanelController_.getPlanetGUI()[i].draw(g);
		}
		g.drawImage(menu_, 0, getHeight() - 30, this);
		menuButton_.draw(g);
		
		if(!gamePanelController_.getFleetGUIs().isEmpty()){
			for(int i = 0; i < gamePanelController_.getFleetsize_(); i++){
				gamePanelController_.getFleetGUIs().get(i).draw(g);
			}
		}
	}
	
	public void setLabelText(String a){
		label.setText(a);
	}
	
	public boolean isLittleON() {
		return littlePanel_;
	}
	
	public void setLittleON(boolean littlePanel_) {
		this.littlePanel_ = littlePanel_;
	}

}
